#pragma once

#include "kernel/interfaces/IConfiguration.h"
#include "iconfiguration_provider.h"
#include "iconfiguration_validator.h"
#include <QHash>
#include <QVector>
#include <QMutex>
#include <QMutexLocker>
#include <memory>

namespace infrastructure {

// 前向声明
class CompositeConfigurationProvider;

/**
 * @brief 配置节实现
 */
class ConfigurationSection : public kernel::IConfigurationSection {
public:
    ConfigurationSection(const QString& path, const QHash<QString, QVariant>& data, 
                        std::shared_ptr<CompositeConfigurationProvider> provider);

    // IConfiguration interface
    QString getString(const QString& key, const QString& defaultValue = "") const override;
    int getInt(const QString& key, int defaultValue = 0) const override;
    bool getBool(const QString& key, bool defaultValue = false) const override;
    double getDouble(const QString& key, double defaultValue = 0.0) const override;
    
    // 写入方法
    bool setString(const QString& key, const QString& value) override;
    bool setInt(const QString& key, int value) override;
    bool setBool(const QString& key, bool value) override;
    bool setDouble(const QString& key, double value) override;
    bool removeKey(const QString& key) override;
    bool save() override;
    
    kernel::SubscriptionHandle subscribe(const QString& key, kernel::ConfigurationChangeCallback callback) override;
    void unsubscribe(kernel::SubscriptionHandle handle) override;
    
    bool isValid() const override;
    infrastructure::ValidationResult getValidationResult() const override;
    std::optional<std::shared_ptr<kernel::IConfigurationSection>> getSection(const QString& key) const override;

    // 配置导入导出方法
    bool exportToFile(const QString& filePath) const override;
    bool importFromFile(const QString& filePath) override;
    QString exportToJson() const override;
    bool importFromJson(const QString& jsonString) override;

    // IConfigurationSection interface
    QString getPath() const override;
    std::optional<QString> getValue() const override;

private:
    QString m_path;
    QHash<QString, QVariant> m_data;
    std::weak_ptr<CompositeConfigurationProvider> m_provider;
    QString buildKey(const QString& key) const;
};

/**
 * @class CompositeConfigurationProvider
 * @brief 复合配置提供者，实现IConfiguration接口，管理多个配置源
 */
class CompositeConfigurationProvider : public kernel::IConfiguration, 
                                     public std::enable_shared_from_this<CompositeConfigurationProvider> {
public:
    CompositeConfigurationProvider();
    virtual ~CompositeConfigurationProvider();

    // IConfiguration interface
    QString getString(const QString& key, const QString& defaultValue = "") const override;
    int getInt(const QString& key, int defaultValue = 0) const override;
    bool getBool(const QString& key, bool defaultValue = false) const override;
    double getDouble(const QString& key, double defaultValue = 0.0) const override;
    
    // 写入方法
    bool setString(const QString& key, const QString& value) override;
    bool setInt(const QString& key, int value) override;
    bool setBool(const QString& key, bool value) override;
    bool setDouble(const QString& key, double value) override;
    bool removeKey(const QString& key) override;
    bool save() override;
    
    kernel::SubscriptionHandle subscribe(const QString& key, kernel::ConfigurationChangeCallback callback) override;
    void unsubscribe(kernel::SubscriptionHandle handle) override;
    
    bool isValid() const override;
    infrastructure::ValidationResult getValidationResult() const override;
    std::optional<std::shared_ptr<kernel::IConfigurationSection>> getSection(const QString& key) const override;

    // 配置导入导出方法
    bool exportToFile(const QString& filePath) const override;
    bool importFromFile(const QString& filePath) override;
    QString exportToJson() const override;
    bool importFromJson(const QString& jsonString) override;

    // 复合配置提供者特有的方法
    void addProvider(std::shared_ptr<IConfigurationProvider> provider);
    void removeProvider(std::shared_ptr<IConfigurationProvider> provider);
    void addValidator(std::shared_ptr<IConfigurationValidator> validator);
    void reload();
    QVector<QString> getAllKeys() const;
    
    // 用于ConfigurationSection
    QVariant getValue(const QString& key) const;
    bool setValue(const QString& key, const QVariant& value);

private:
    struct Subscription {
        kernel::SubscriptionHandle handle;
        QString key;
        kernel::ConfigurationChangeCallback callback;
    };

    mutable QMutex m_mutex;
    QVector<std::shared_ptr<IConfigurationProvider>> m_providers;
    QVector<std::shared_ptr<IConfigurationValidator>> m_validators;
    QHash<QString, QVariant> m_mergedData;
    QVector<Subscription> m_subscriptions;
    ValidationResult m_validationResult;
    kernel::SubscriptionHandle m_nextHandle;
    
    void mergeData();
    void validateConfiguration();
    void notifySubscribers(const QString& key, const QVariant& newValue);
    void onProviderChange(const QString& key, const QVariant& newValue);
    void setupProviderCallbacks();
    QHash<QString, QVariant> getSubData(const QString& prefix) const;
    bool keyExists(const QString& key) const;
    QString normalizeKey(const QString& key) const;
    void parseJsonObject(const QJsonObject& obj, const QString& prefix, QHash<QString, QVariant>& result);
};

} // namespace infrastructure 